﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yeetop.PayHub.ChinaBank;
using Yeetop;
using Yeetop.PayHub.EToPay;
using Yeetop.PayHub.YeePay;

namespace Yeetop.PayHub
{
    /// <summary>
    /// /// <summary>
    /// 接受网关通知的工厂。
    /// Copyright © Yeetop Inc.(www.yeetop.com)  All rights reserved. 
    /// </summary>
    /// </summary>
    internal class PayResultFactory
    {
        Dictionary<string, string> notifyData;

        internal PayResultFactory(Dictionary<string, string> notifyData)
        {
            this.notifyData = notifyData;
        }

        internal ReceiveDataInfo GetPayReturnDate(MerchantInfo merchant, CheckBillDelegate checkBill)
        {
            BasePayment payment = null;

            switch (merchant.PayPlatform)
            {
                case PayPlatformType.ChinaBank:
                    payment = new ChinaBankPayment();
                    break;
                case PayPlatformType.EToPay:
                    payment = new EToPayPayment();
                    break;
                case PayPlatformType.YeePay:
                    payment = new YeePayPayment();
                    break;
            }

            if (payment != null)
            {
                payment.ReceiveData.NotifyData = notifyData;
                payment.Merchant = merchant;

                payment.ReceiveData.PayStatus = payment.ValidateReturn(checkBill);

                payment.ReceiveData.PayPlatform = payment.PayPlatform;

                return payment.ReceiveData;
            }

            return new ReceiveDataInfo();
        }

        internal bool GetPayNotifyData(MerchantInfo merchant, CheckUpdateBillDelegate checkUpdateBill)
        {
            BasePayment payment = null;

            switch (merchant.PayPlatform)
            {
                case PayPlatformType.ChinaBank:
                    payment = new ChinaBankPayment();
                    break;
                case PayPlatformType.EToPay:
                    payment = new EToPayPayment();
                    break;
                case PayPlatformType.YeePay:
                    payment = new YeePayPayment();
                    break;
            }


            if (payment != null)
            {
                payment.ReceiveData.NotifyData = notifyData;
                payment.Merchant = merchant;

                return payment.ValidateNotify(checkUpdateBill);
            }

            return false;
        }

        internal PayPlatformType PayPlatform()
        {
            if (IsChinabank)
            {
                return PayPlatformType.ChinaBank;
            }
            else if (IsEToPay)
            {
                return PayPlatformType.EToPay;
            }
            else if (IsYeePay)
            {
                return PayPlatformType.YeePay;
            }

            return PayPlatformType.Error;
        }

        /// <summary>
        /// 是否是Chinabank网关
        /// </summary>
        private bool IsChinabank
        {
            get
            {
                string[] verifyKeys = { "v_oid", "v_pstatus", "v_amount", "v_moneytype" };
                return notifyData.ContainsKeys(verifyKeys);
            }
        }

        private bool IsEToPay
        {
            get
            {
                string[] verifyKeys = { "p01_service", "p02_out_ordercode", "p03_payamount", "p04_sitecode" };
                return notifyData.ContainsKeys(verifyKeys);
            }
        }

        private bool IsYeePay
        {
            get
            {
                string[] verifyKeys = { "p1_MerId", "r0_Cmd", "r1_Code", "r3_Amt" };
                return notifyData.ContainsKeys(verifyKeys);
            }
        }
    }
}
